package org.jeecg.modules.solitaire_app.app.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.solitaire_app.app.entity.VO.HomeResultVO;
import org.jeecg.modules.solitaire_app.app.entity.VO.PriceAvgVO;
import org.jeecg.modules.solitaire_app.app.entity.VO.RefineryPriceChartVO;
import org.jeecg.modules.solitaire_app.app.entity.VO.RefinerytzVO;
import org.jeecg.modules.solitaire_app.app.entity.YsyRefineryPrice;
import org.jeecg.modules.solitaire_app.app.mapper.YsyRefineryPriceMapper;
import org.jeecg.modules.solitaire_app.app.query.RefineryChartQuery;
import org.jeecg.modules.solitaire_app.app.query.RefineryPriceQuery;
import org.jeecg.modules.solitaire_app.app.query.RefinerytzQuery;
import org.jeecg.modules.solitaire_app.app.service.IYsyRefineryPriceService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 炼厂油品报价
 * @Author: jeecg-boot
 * @Date: 2024-03-25
 * @Version: V1.0
 */
@Service
@Slf4j
@DS("ysy_db")
public class YsyRefineryPriceServiceImpl extends ServiceImpl<YsyRefineryPriceMapper, YsyRefineryPrice> implements IYsyRefineryPriceService {

    @Override
    public Result<PageInfo<HomeResultVO>> getHomeResult(RefineryPriceQuery refineryPriceQuery) {
        Result<PageInfo<HomeResultVO>> result = new Result<>();
        try {
            PageHelper.startPage(refineryPriceQuery.getPageNo(), refineryPriceQuery.getPageSize());
            List<HomeResultVO> homeResultVOS = this.baseMapper.getHomeResult(refineryPriceQuery);
            PageInfo pageInfo = new PageInfo(homeResultVOS);
            result.setSuccess(true);
            result.setCode(200);
            result.setResult(pageInfo);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("首页查询成品油报价响应失败", e.getMessage());
            result.setMessage("服务器响应失败");
            result.setCode(500);
            result.setSuccess(false);
            return result;
        }
    }

    @Override
    public Result<List<RefinerytzVO>> getRefineryZtData(RefinerytzQuery refinerytzQuery) {
        Result<List<RefinerytzVO>> result = new Result<>();
        List<RefinerytzVO> refinerytzVOS = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            List<YsyRefineryPrice> ysyRefineryPrices = this.baseMapper.getRefineryZtData(refinerytzQuery);

            if (ysyRefineryPrices.size() == 0) {
                result.setMessage("该炼厂今日未报价");
            }

            if (ysyRefineryPrices.size() == 1) {
                RefinerytzVO refinerytzVO = new RefinerytzVO();
                refinerytzVO.setPrice(ysyRefineryPrices.get(0).getPrice() + "");
                refinerytzVO.setPriceChg("--");
                refinerytzVO.setTime(sdf.format(ysyRefineryPrices.get(0).getCreateTime()));
                refinerytzVOS.add(refinerytzVO);

                RefinerytzVO refinerytzVO1 = new RefinerytzVO();
                refinerytzVO1.setPrice("--");
                refinerytzVO1.setPriceChg("--");
                refinerytzVO1.setTime("--");
                refinerytzVOS.add(refinerytzVO1);

                RefinerytzVO refinerytzVO2 = new RefinerytzVO();
                refinerytzVO2.setPrice("--");
                refinerytzVO2.setPriceChg("--");
                refinerytzVO2.setTime("--");
                refinerytzVOS.add(refinerytzVO2);

                RefinerytzVO refinerytzVO3 = new RefinerytzVO();
                refinerytzVO3.setPrice("--");
                refinerytzVO3.setPriceChg("--");
                refinerytzVO3.setTime("--");
                refinerytzVOS.add(refinerytzVO3);

                RefinerytzVO refinerytzVO4 = new RefinerytzVO();
                refinerytzVO4.setPrice(ysyRefineryPrices.get(0).getPrice() + "");
                refinerytzVO4.setPriceChg("--");
                refinerytzVO4.setTime(sdf.format(ysyRefineryPrices.get(0).getCreateTime()));
                refinerytzVOS.add(refinerytzVO4);
            }

            if (ysyRefineryPrices.size() == 2) {
                //初价
                RefinerytzVO refinerytzVO = new RefinerytzVO();
                refinerytzVO.setPrice(ysyRefineryPrices.get(0).getPrice() + "");
                refinerytzVO.setPriceChg("--");
                refinerytzVO.setTime(sdf.format(ysyRefineryPrices.get(0).getCreateTime()));
                refinerytzVOS.add(refinerytzVO);

                //一调
                RefinerytzVO refinerytzVO1 = new RefinerytzVO();
                refinerytzVO1.setPrice(ysyRefineryPrices.get(1).getPrice() + "");
                refinerytzVO1.setPriceChg(ysyRefineryPrices.get(1).getChg() + "");
                refinerytzVO1.setTime(sdf.format(ysyRefineryPrices.get(1).getCreateTime()));
                refinerytzVOS.add(refinerytzVO1);

                //二调
                RefinerytzVO refinerytzVO2 = new RefinerytzVO();
                refinerytzVO2.setPrice("--");
                refinerytzVO2.setPriceChg("--");
                refinerytzVO2.setTime("--");
                refinerytzVOS.add(refinerytzVO2);

                //三调
                RefinerytzVO refinerytzVO3 = new RefinerytzVO();
                refinerytzVO3.setPrice("--");
                refinerytzVO3.setPriceChg("--");
                refinerytzVO3.setTime("--");
                refinerytzVOS.add(refinerytzVO3);

                //最新价
                RefinerytzVO refinerytzVO4 = new RefinerytzVO();
                refinerytzVO4.setPrice(ysyRefineryPrices.get(1).getPrice() + "");
                refinerytzVO4.setPriceChg((ysyRefineryPrices.get(1).getPrice() - ysyRefineryPrices.get(0).getPrice()) + "");
                refinerytzVO4.setTime(sdf.format(ysyRefineryPrices.get(1).getCreateTime()));
                refinerytzVOS.add(refinerytzVO4);
            }

            if (ysyRefineryPrices.size() == 3) {
                //初价
                RefinerytzVO refinerytzVO = new RefinerytzVO();
                refinerytzVO.setPrice(ysyRefineryPrices.get(0).getPrice() + "");
                refinerytzVO.setPriceChg("--");
                refinerytzVO.setTime(sdf.format(ysyRefineryPrices.get(0).getCreateTime()));
                refinerytzVOS.add(refinerytzVO);

                //一调
                RefinerytzVO refinerytzVO1 = new RefinerytzVO();
                refinerytzVO1.setPrice(ysyRefineryPrices.get(1).getPrice() + "");
                refinerytzVO1.setPriceChg(ysyRefineryPrices.get(1).getChg() + "");
                refinerytzVO1.setTime(sdf.format(ysyRefineryPrices.get(1).getCreateTime()));
                refinerytzVOS.add(refinerytzVO1);

                //二调
                RefinerytzVO refinerytzVO2 = new RefinerytzVO();
                refinerytzVO2.setPrice(ysyRefineryPrices.get(2).getPrice() + "");
                refinerytzVO2.setPriceChg(ysyRefineryPrices.get(2).getChg() + "");
                refinerytzVO2.setTime(sdf.format(ysyRefineryPrices.get(2).getCreateTime()));
                refinerytzVOS.add(refinerytzVO2);

                //三调
                RefinerytzVO refinerytzVO3 = new RefinerytzVO();
                refinerytzVO3.setPrice("--");
                refinerytzVO3.setPriceChg("--");
                refinerytzVO3.setTime("--");
                refinerytzVOS.add(refinerytzVO3);

                //最新价
                RefinerytzVO refinerytzVO4 = new RefinerytzVO();
                refinerytzVO4.setPrice(ysyRefineryPrices.get(2).getPrice() + "");
                refinerytzVO4.setPriceChg(ysyRefineryPrices.get(2).getChg() + "");
                refinerytzVO4.setTime(sdf.format(ysyRefineryPrices.get(2).getCreateTime()));
                refinerytzVOS.add(refinerytzVO4);
            }

            if (ysyRefineryPrices.size() >= 4) {
                //初价
                RefinerytzVO refinerytzVO = new RefinerytzVO();
                refinerytzVO.setPrice(ysyRefineryPrices.get(0).getPrice() + "");
                refinerytzVO.setPriceChg("--");
                refinerytzVO.setTime(sdf.format(ysyRefineryPrices.get(0).getCreateTime()));
                refinerytzVOS.add(refinerytzVO);

                //一调
                RefinerytzVO refinerytzVO1 = new RefinerytzVO();
                refinerytzVO1.setPrice(ysyRefineryPrices.get(1).getPrice() + "");
                refinerytzVO1.setPriceChg(ysyRefineryPrices.get(1).getChg() + "");
                refinerytzVO1.setTime(sdf.format(ysyRefineryPrices.get(1).getCreateTime()));
                refinerytzVOS.add(refinerytzVO1);

                //二调
                RefinerytzVO refinerytzVO2 = new RefinerytzVO();
                refinerytzVO2.setPrice(ysyRefineryPrices.get(2).getPrice() + "");
                refinerytzVO2.setPriceChg(ysyRefineryPrices.get(2).getChg() + "");
                refinerytzVO2.setTime(sdf.format(ysyRefineryPrices.get(2).getCreateTime()));
                refinerytzVOS.add(refinerytzVO2);

                //三调
                RefinerytzVO refinerytzVO3 = new RefinerytzVO();
                refinerytzVO3.setPrice(ysyRefineryPrices.get(ysyRefineryPrices.size() - 1).getPrice() + "");
                refinerytzVO3.setPriceChg(ysyRefineryPrices.get(ysyRefineryPrices.size() - 1).getChg() + "");
                refinerytzVO3.setTime(sdf.format(ysyRefineryPrices.get(ysyRefineryPrices.size() - 1).getCreateTime()));
                refinerytzVOS.add(refinerytzVO3);

                RefinerytzVO refinerytzVO4 = new RefinerytzVO();
                refinerytzVO4.setPrice(ysyRefineryPrices.get(ysyRefineryPrices.size() - 1).getPrice() + "");
                refinerytzVO4.setPriceChg(ysyRefineryPrices.get(ysyRefineryPrices.size() - 1).getChg() + "");
                refinerytzVO4.setTime(sdf.format(ysyRefineryPrices.get(ysyRefineryPrices.size() - 1).getCreateTime()));
                refinerytzVOS.add(refinerytzVO4);
            }

            for (int i = 0; i < refinerytzVOS.size(); i++) {
                refinerytzVOS.get(i).setName(i == 0 ? "初价" : i == 1 ? "一调" : i == 2 ? "二调" : i == 3 ? "三调" : "最新油价");
            }

            result.setCode(200);
            result.setSuccess(true);
            result.setResult(refinerytzVOS);
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询炼厂调整价格响应失败");
            result.setMessage("服务器响应失败");
            result.setCode(500);
            result.setSuccess(false);
            return result;
        }
    }

    @Override
    public Result<List<RefineryPriceChartVO>> getRefineryPriceChart(RefineryChartQuery refineryChartQuery) {
        Result<List<RefineryPriceChartVO>> result = new Result<>();
        try {
            List<YsyRefineryPrice> refineryPrices = this.baseMapper.getRefineryPriceChart(refineryChartQuery);
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
            SimpleDateFormat sdfDay = new SimpleDateFormat("HH:mm");
            List<RefineryPriceChartVO> refineryPriceChartVOS = new ArrayList<>();

            if (refineryPriceChartVOS.size() < 6 && refineryChartQuery.getType() != 3) {
                refineryPrices.stream().forEach(item -> {

                    RefineryPriceChartVO priceChartVO = new RefineryPriceChartVO();
                    priceChartVO.setPrice(item.getPrice());
                    priceChartVO.setDateTime(refineryChartQuery.getType() == 1
                            ? sdfDay.format(item.getCreateTime())
                            : refineryChartQuery.getType() == 2 ?
                            sdf.format(item.getCreateTime()) : sdf.format(item.getCreateTime()));
                    refineryPriceChartVOS.add(priceChartVO);
                });
            }

            if (refineryChartQuery.getType() == 3) {
                if (refineryPrices.size() <= 6) {
                    refineryPrices.stream().forEach(item -> {
                        RefineryPriceChartVO priceChartVO = new RefineryPriceChartVO();
                        priceChartVO.setPrice(item.getPrice());
                        priceChartVO.setDateTime(refineryChartQuery.getType() == 1
                                ? sdfDay.format(item.getCreateTime())
                                : refineryChartQuery.getType() == 2 ?
                                sdf.format(item.getCreateTime()) : sdf.format(item.getCreateTime()));
                        refineryPriceChartVOS.add(priceChartVO);
                    });
                } else {
                    List<YsyRefineryPrice> refineryPriceList = selectItemsProportionally(refineryPrices, 6);
                    refineryPriceList.stream().forEach(item -> {
                        RefineryPriceChartVO priceChartVO = new RefineryPriceChartVO();
                        priceChartVO.setPrice(item.getPrice());
                        priceChartVO.setDateTime(refineryChartQuery.getType() == 1
                                ? sdfDay.format(item.getCreateTime())
                                : refineryChartQuery.getType() == 2 ?
                                sdf.format(item.getCreateTime()) : sdf.format(item.getCreateTime()));
                        refineryPriceChartVOS.add(priceChartVO);
                    });
                }
            }
            result.setCode(200);
            result.setResult(refineryPriceChartVOS);
            result.setSuccess(true);
            return result;
        } catch (Exception e) {
            result.setMessage("服务器响应失败");
            result.setCode(500);
            result.setSuccess(false);
            return result;
        }
    }

    @Override
    public Result<PageInfo<YsyRefineryPrice>> getOilTypePriceHis(Map<String, Object> map) {
        return null;
    }

    @Override
    public Result<List<PriceAvgVO>> getPriceAvg() {
        Result<List<PriceAvgVO>> result = new Result<>();
        result.setCode(200);
        result.setSuccess(true);
        result.setResult(this.baseMapper.getPriceAvg());
        return result;
    }

    public List<YsyRefineryPrice> selectItemsProportionally(List<YsyRefineryPrice> array, int count) {
        List<YsyRefineryPrice> ysyRefineryPrices = new ArrayList<>();
        int length = array.size();
        // 计算步长
        double step = (double) length / count;

        // 使用步长选择元素
        for (int i = 0; i < count; i++) {
            int index = (int) Math.round(step * i); // 四舍五入取整
            ysyRefineryPrices.add(array.get(index));
        }
        return ysyRefineryPrices;
    }
}
